package  
{
	import com.buraks.utils.fastmem;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.TimerEvent;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	import flash.utils.Timer;
	import starling.core.Starling;
	import starling.display.BlendMode;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.textures.Texture;
	
	public class BGManager 
	{
		private var mStageWidth:int = 0;
		private var mStageHeight:int = 0;
		private var mColorList:Vector.<ByteArray> = null;
		private var mRawImageList:Array = null;
		private var mContainer:Sprite = null;
		private var mImageList:Vector.<Image> = new Vector.<Image>();
		private var mIndex:int = 0;
		private var mTimer:Timer = new Timer(10000);
		private const DATA_SIZE:int = 1;
		
		public function BGManager() {}
		
		public function Init(aRawImageList:Array, aContainer:Sprite):void
		{
			mStageWidth = Starling.current.stage.stageWidth;
			mStageHeight = Starling.current.stage.stageHeight;
			mRawImageList = aRawImageList;
			mContainer = aContainer;
			mColorList = new Vector.<ByteArray>(mRawImageList.length, true);
			
			for (var i:int = 0; i < mRawImageList.length; i++)
			{
				mImageList.push(new Image(Texture.fromBitmap(mRawImageList[i], false, true)));
				mImageList[i].blendMode = BlendMode.NONE;
				mImageList[i].touchable = false;
				
				mColorList[i] = ProcessBmpData(mRawImageList[i].bitmapData);
			}
			
			MAX_POS = mColorList[mIndex].length;
			fastmem.fastSelectMem(mColorList[mIndex]);
			
			//mContainer.addChild(mImageList[mIndex]);
			mTimer.addEventListener(TimerEvent.TIMER, OnTimer);
			mTimer.start();
		}
		
		private function OnTimer(aEvent:TimerEvent):void
		{
			mIndex++;
			
			if (mIndex >= mRawImageList.length)
			{
				mIndex = 0;
			}
			
			fastmem.fastDeselectMem();
			MAX_POS = mColorList[mIndex].length;
			fastmem.fastSelectMem(mColorList[mIndex]);
		}
		
		private const COLOR_FORCE:Number = 1;
		private var mPixelColor:uint = 0;
		private var mBoidColor:uint = 0;
		private var mPixelR:uint = 0;
		private var mPixelG:uint = 0;
		private var mPixelB:uint = 0;
		private var mBoidR:uint = 0;
		private var mBoidG:uint = 0;
		private var mBoidB:uint = 0;
		private var MAX_POS:uint = 0;
		public function ApplyBoidColor(aBoid:Boid):void
		{
			var aX:int = aBoid.mPos.x;
			var aY:int = aBoid.mPos.y;
			
			if (aX < 0 || aX > mStageWidth) 	return;
			if (aY < 0 || aY > mStageHeight) 	return;
			
			var pos:int = ((aY * mStageWidth) + aX) * (DATA_SIZE * 3);
			
			if (pos > MAX_POS) return;
			
			mPixelR = fastmem.fastGetByte(pos); pos += DATA_SIZE;
			mPixelG = fastmem.fastGetByte(pos); pos += DATA_SIZE;
			mPixelB = fastmem.fastGetByte(pos);
			
			mBoidR = ((mBoidColor >> 16) & 0xFF);
			mBoidG = ((mBoidColor >> 8) & 0xFF);
			mBoidB = (mBoidColor & 0xFF);
			
			mBoidR += (mPixelR - mBoidR); if (mBoidR > 255) mBoidR = 255; else if(mBoidR < 0) mBoidR = 0;
			mBoidG += (mPixelG - mBoidG); if (mBoidG > 255) mBoidG = 255; else if(mBoidG < 0) mBoidG = 0;
			mBoidB += (mPixelB - mBoidB); if (mBoidB > 255) mBoidB = 255; else if(mBoidB < 0) mBoidB = 0;
			
			aBoid.mColor = (mBoidR << 16) | (mBoidG << 8) | mBoidB;
		}
		
		private function ProcessBmpData(aBitmapData:BitmapData):ByteArray
		{
			var result:ByteArray = new ByteArray();
			result.length 	= mStageWidth * mStageHeight * (DATA_SIZE * 3);
			result.position = 0;
			var pos:uint 	= 0;
			var color:uint 	= 0;
			var r:uint 		= 0;
			var g:uint 		= 0;
			var b:uint 		= 0;
			
			fastmem.fastSelectMem(result);
			for (var i:int = 0; i < mStageHeight; i++)
			{
				for (var j:int = 0; j < mStageWidth; j++)
				{
					color = aBitmapData.getPixel(j, i);
					
					r = ((color >> 16) & 0xFF);
					g = ((color >> 8) & 0xFF);
					b = (color & 0xFF);
					fastmem.fastSetByte(r, pos);
					pos += DATA_SIZE;
					fastmem.fastSetByte(g, pos);
					pos += DATA_SIZE;
					fastmem.fastSetByte(b, pos);
					pos += DATA_SIZE;
				}
			}
			fastmem.fastDeselectMem();
			
			return result;
		}
	}
}